<template>
  <div class="goods-comment" v-if="commentInfo">
      <!-- 头部信息 -->
    <div class="head">
      <div class="data">
        <p><span>{{commentInfo.evaluateCount}}</span><span>人购买</span></p>
        <p><span>{{commentInfo.praisePercent}}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <!-- <a href="javascript:;" :class="{active:}">全部评价（{{commentInfo.salesCount}}）</a> -->
          <a href="javascript:;" v-for="(item,i) in tags" @click="changeTag(i)" :class="{active:index === i}" :key="item">{{item.title}}（{{item.tagCount}}）</a>
        </div>
      </div>
    </div>
    <!-- /头部信息 -->

    <!-- 排序信息 -->
    <div class="sort">
      <span>排序：</span>
      <a @click="changeSort(null)" href="javascript:;"  :class="{active:reqParams.sortField === null}">默认</a>
      <a @click="changeSort('createTime')" href="javascript:;" :class="{active:reqParams.sortField === 'createTime'}">最新</a>
      <a @click="changeSort('praiseCount')" href="javascript:;" :class="{active:reqParams.sortField === 'praiseCount'}">最热</a>
    </div>
    <!-- /排序信息 -->
    <div class="list"></div>
    <!-- 列表信息 -->
    <!-- 列表 -->
    <div class="list" v-if="commentList">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{formateNickname(item.member.nickname)}}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="i in item.score" :key="i='s'"></i>
            <i class="iconfont icon-wjx02" v-for="i in (5-item.score)" :key="i+'k'"></i>
            <span class="attr" v-for="(sub,i) in item.orderInfo.specs" :key="i">{{`${sub.name}: ${sub.nameValue}`}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <!-- 评论图片 -->
          <GoodsCommentImage v-if="item.pictures.length" :pictures="item.pictures" />
          <div class="time">
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
      <!-- 分页 -->
      <XtxPagination v-if="total > 0" @currentChange="changePagerFn" :total="total" :page-size="reqParams.pageSize" :current-page="reqParams.page"/>
      <!-- /分页 -->
    </div>
    <!-- /列表信息 -->
  </div>
</template>
<script>
import { inject, reactive, ref, watch } from 'vue'
import { findGoodsCommentInfo, findGoodsCommentList } from '@/api/product'
import { useRoute } from 'vue-router'
import GoodsCommentImage from './goods-comment-image.vue'
export default {
  name: 'GoodsComment',
  components: {
    GoodsCommentImage
  },
  setup () {
    const commentInfo = ref(null)
    const goods = inject('goods')
    const index = ref(0)
    const tags = ref([])
    findGoodsCommentInfo(goods.value.id).then(data => {
      commentInfo.value = data.result
      tags.value.push({ title: '全部', tagCount: data.result.salesCount, type: 'all' }, { title: '有图', tagCount: data.result.hasPictureCount, type: 'img' })
      data.result.tags.forEach(item => {
        tags.value.push(item)
      })
      // console.log(data.result)
    })
    // 激活tag
    const changeTag = (i) => {
      index.value = i
      const tag = tags.value[i]
      // console.log(tag)

      // 带年纪tag的时候修改筛选条件
      // 情况:全部评价
      // 镜框2：有图
      // 精矿:正常tag
      if (tag.type === 'all') {
        reqParams.hasPicture = null
        reqParams.tag = null
      } else if (tag.type === 'img') {
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        reqParams.hasPicture = null
        reqParams.tag = tag.title
      }
      // 页码重置为1
      reqParams.page = 1
    }

    // 准备筛选条件数据
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      // 排序方式:praiseCount 热度  createTime 最新
      sortField: null

    })

    // 初始化需要发请求，筛选条件发生变化也要发请求
    const commentList = ref([])
    const total = ref(0)
    const route = useRoute()
    watch(reqParams, () => {
      // console.log('发请求')
      // 页码重置到1
      findGoodsCommentList(route.params.id, reqParams).then(data => {
        commentList.value = data.result.items
        total.value = data.result.counts
        // console.log(data.result.items)
      })
    }, { immediate: true })

    // 更改标签
    const changeSort = (tag) => {
      reqParams.sortField = tag
      reqParams.page = 1
    }

    // 定义转换数据的函数(对应vue2.0的过滤器)
    // const  formatSpecs = (specs) => {
    //   return specs.reduce((p,c) => {
    //     `${p}   ${c.name}: ${c.nameValue}`
    //   },'')
    // }

    const formateNickname = (nickname) => {
      return nickname.substr(0, 1) + '****' + nickname.substr(-1)
    }

    // 实现分页切换
    const changePagerFn = (newPage) => {
      reqParams.page = newPage
    }

    return { commentInfo, tags, index, changeTag, reqParams, total, commentList, changeSort, formateNickname, changePagerFn }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor,50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,&:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
